package hos.intent;

import android.app.AlertDialog;
import android.app.AppOpsManager;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * <p>Title: SettingCan </p>
 * <p>Description: 设置页面判断 </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-20 22:19
 */
public class IntentAvailable {

    private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
    private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

    /**
     * 判断是否可以发送通知
     *
     * @param context 上下文
     */
    public static boolean notificationAvailable(Context context) {
        NotificationManager manager = (NotificationManager) context.getSystemService(
                Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= 24) {
            return manager.areNotificationsEnabled();
        } else if (Build.VERSION.SDK_INT >= 19) {
            AppOpsManager appOps =
                    (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            ApplicationInfo appInfo = context.getApplicationInfo();
            String pkg = context.getApplicationContext().getPackageName();
            int uid = appInfo.uid;
            try {
                Class<?> appOpsClass = Class.forName(AppOpsManager.class.getName());
                Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE,
                        Integer.TYPE, String.class);
                Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
                int value = (int) opPostNotificationValue.get(Integer.class);
                return ((int) checkOpNoThrowMethod.invoke(appOps, value, uid, pkg)
                        == AppOpsManager.MODE_ALLOWED);
            } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException
                     | InvocationTargetException | IllegalAccessException | RuntimeException e) {
                return true;
            }
        } else {
            return true;
        }
    }

    /**
     * 判断是否可以使用系统级别的弹出窗口
     *
     * @param context 上下文
     */
    public static boolean systemDialogAvailable(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return Settings.canDrawOverlays(context);
        }
        return true;
    }

    /**
     * 判断设置是否可以写入
     *
     * @param context 上下文
     */
    public static boolean writeSettingAvailable(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return Settings.System.canWrite(context);
        }
        return true;
    }

    /**
     * 判断储存空间是否可以使用
     *
     * @return true 可以使用
     */
    public static boolean storageAvailable() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            return Environment.isExternalStorageManager();
        }
        return true;
    }

    /**
     * 判断是否运行安装第三方应用
     *
     * @param context 上下文
     */
    public static boolean installAvailable(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return context.getPackageManager().canRequestPackageInstalls();
        }
        return true;
    }

    /**
     * GPS是否可以使用
     *
     * @param context 上下文
     */
    public static boolean locationAvailable(Context context) {
        if (context == null) {
            return false;
        }
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            return locationManager.isLocationEnabled();
        } else {
            return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
                    || locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        }
    }

    /**
     * 是否有定位功能
     *
     * @param context 上下文
     */
    public static boolean systemLocationAvailable(Context context) {
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION);
    }

    public static void showSettingTip(Context context, String message,
                                      DialogInterface.OnClickListener listener) {
        showSettingTip(context, message, listener, null);
    }

    public static void showSettingTip(Context context, String message,
                                      DialogInterface.OnClickListener listener,
                                      DialogInterface.OnClickListener cancelListener) {
        showSettingTip(context, message, "去设置", "暂不开启", listener, cancelListener);
    }

    public static void showSettingTip(Context context, String message, String ok, String cancel,
                                      DialogInterface.OnClickListener listener) {
        showSettingTip(context, message, ok, cancel, listener, null);
    }

    public static void showSettingTip(Context context, String message, String ok, String cancel,
                                      DialogInterface.OnClickListener listener,
                                      DialogInterface.OnClickListener cancelListener) {
        new AlertDialog.Builder(context)
                .setTitle("温馨提示")
                .setMessage(message)
                .setCancelable(false)
                .setPositiveButton(ok, listener)
                .setNegativeButton(cancel, cancelListener)
                .show();
    }

    /**
     * Intent 是否可以使用
     *
     * @param context 上下文
     */
    public static boolean isIntentAvailable(Context context, Intent intent) {
        return intent.resolveActivity(context.getPackageManager()) != null;
    }

    /**
     * Intent 是否可以使用
     *
     * @param context 上下文
     * @param action  行为
     */
    public static boolean isIntentAvailable(Context context, String action) {
        return new Intent(action).resolveActivity(context.getPackageManager()) != null;
    }
}
